package cn.iocoder.yudao.module.agent.convert.brokeragewithdrawrecord;

import java.util.*;

import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.framework.dict.core.util.DictFrameworkUtils;
import cn.iocoder.yudao.module.agent.base.MemberRespVO;
import cn.iocoder.yudao.module.agent.controller.admin.user.vo.AgentUserRespVO;
import cn.iocoder.yudao.module.agent.controller.admin.withdrawaccount.vo.AgentWithdrawAccountRespVO;
import cn.iocoder.yudao.module.agent.controller.app.brokeragewithdrawrecord.vo.AppAgentBrokerageWithdrawRecordCreateReqVO;
import cn.iocoder.yudao.module.agent.convert.base.AgentConvert;
import cn.iocoder.yudao.module.agent.convert.withdrawaccount.AgentWithdrawAccountConvert;
import cn.iocoder.yudao.module.agent.dal.dataobject.brokeragerecord.AgentBrokerageRecordDO;
import cn.iocoder.yudao.module.agent.dal.dataobject.user.AgentUserDO;
import cn.iocoder.yudao.module.agent.dal.dataobject.withdrawaccount.AgentWithdrawAccountDO;
import cn.iocoder.yudao.module.agent.enums.AgentBrokerageWithdrawStatusEnum;
import cn.iocoder.yudao.module.member.api.user.dto.MemberUserRespDTO;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;
import cn.iocoder.yudao.module.agent.controller.admin.brokeragewithdrawrecord.vo.*;
import cn.iocoder.yudao.module.agent.dal.dataobject.brokeragewithdrawrecord.AgentBrokerageWithdrawRecordDO;
import org.springframework.dao.RecoverableDataAccessException;

/**
 * 代理人佣金提现记录 Convert
 *
 * @author 曹建伟
 */
@Mapper
public interface AgentBrokerageWithdrawRecordConvert {

    AgentBrokerageWithdrawRecordConvert INSTANCE = Mappers.getMapper(AgentBrokerageWithdrawRecordConvert.class);

    AgentBrokerageWithdrawRecordDO convert(AgentBrokerageWithdrawRecordCreateReqVO bean);

    AgentBrokerageWithdrawRecordDO convert(AgentBrokerageWithdrawRecordUpdateReqVO bean);

    AgentBrokerageWithdrawRecordRespVO convert(AgentBrokerageWithdrawRecordDO bean);

    List<AgentBrokerageWithdrawRecordRespVO> convertList(List<AgentBrokerageWithdrawRecordDO> list);

    PageResult<AgentBrokerageWithdrawRecordRespVO> convertPage(PageResult<AgentBrokerageWithdrawRecordDO> page);

    List<AgentBrokerageWithdrawRecordExcelVO> convertList02(List<AgentBrokerageWithdrawRecordDO> list);

    default AgentBrokerageWithdrawRecordDO convert(AgentUserDO agentUserDO, AgentWithdrawAccountDO accountDO, AppAgentBrokerageWithdrawRecordCreateReqVO reqVO, Integer feePrice){
        AgentBrokerageWithdrawRecordDO.AgentBrokerageWithdrawRecordDOBuilder recordDO = AgentBrokerageWithdrawRecordDO.builder();


        recordDO.userId(agentUserDO.getUserId());
        recordDO.price(reqVO.getPrice());
        recordDO.feePrice(feePrice);
        recordDO.leftBrokerage(agentUserDO.getBrokerage() - reqVO.getPrice());

        return recordDO.build();
    }

    default AgentBrokerageWithdrawRecordRespVO convert02(AgentBrokerageWithdrawRecordDO bean, MemberRespVO memberRespVO, AgentWithdrawAccountRespVO accountRespVO){
        if ( bean == null ) {
            return null;
        }

        AgentBrokerageWithdrawRecordRespVO recordRespVO = new AgentBrokerageWithdrawRecordRespVO();

        recordRespVO.setId(bean.getId());
        recordRespVO.setLeftBrokerage(bean.getLeftBrokerage());
        recordRespVO.setUserId(bean.getUserId());
        recordRespVO.setAuditReason(bean.getAuditReason());
        recordRespVO.setAuditTime(bean.getAuditTime());
        recordRespVO.setFeePrice(bean.getFeePrice());
        recordRespVO.setPrice(bean.getPrice());
        recordRespVO.setStatus(bean.getStatus());
        recordRespVO.setStatusName(DictFrameworkUtils.getDictDataLabel("agent_brokerage_withdraw_status", bean.getStatus()));
        recordRespVO.setCreateTime(bean.getCreateTime());

        recordRespVO.setAgentMember(memberRespVO);
        recordRespVO.setAccountInfo(accountRespVO);

        return recordRespVO;
    }

    default List<AgentBrokerageWithdrawRecordRespVO> convertList02(List<AgentBrokerageWithdrawRecordDO> recordDOList, Map<Long, MemberUserRespDTO> memberMap, Map<Long, AgentWithdrawAccountDO> accountMap) {
        List<AgentBrokerageWithdrawRecordRespVO> list = new ArrayList<>();
        recordDOList.forEach(recordDO -> {
            AgentWithdrawAccountRespVO accountRespVO = AgentWithdrawAccountConvert.INSTANCE.convert(accountMap.get(recordDO.getUserId()));
            MemberRespVO memberRespVO = AgentConvert.INSTANCE.covnertMember(memberMap.get(recordDO.getUserId()));
            list.add(convert02(recordDO, memberRespVO, accountRespVO));
        });

        return list;
    }

    default PageResult<AgentBrokerageWithdrawRecordRespVO> convertPage02(PageResult<AgentBrokerageWithdrawRecordDO> page, Map<Long, MemberUserRespDTO> memberMap, Map<Long, AgentWithdrawAccountDO> accountMap){
        if ( page == null ) {
            return null;
        }

        PageResult<AgentBrokerageWithdrawRecordRespVO> pageResult = new PageResult<AgentBrokerageWithdrawRecordRespVO>();
        List<AgentBrokerageWithdrawRecordRespVO> list = convertList02(page.getList(), memberMap, accountMap);

        pageResult.setList(list);
        pageResult.setTotal(page.getTotal());

        return pageResult;
    }

}
